<!--
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-10 11:18:43
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-25 21:54:15
 * @FilePath: /webServerAdmin/src/views/system/departmentManage/components/edit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-drawer v-model="visible">
    <el-form ref="formRef" :model="modelValue" :rules="rules" label-position="top">
      <el-form-item label="上级部门" prop="parent">
        <el-tree-select
          style="width: 100%"
          show-checkbox
          v-model="modelValue.parent!.id"
          :data="deptOpt"
          check-strictly
          node-key="id"
          clearable
          :multiple="false"
          :props="{
            label: 'deptName'
          }"
          :render-after-expand="false"
          check-on-click-node
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="modelValue.deptName"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="deptCode">
        <el-input v-model="modelValue.deptCode"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <div>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { editDept, fetchDeptInfo, fetchDeptTrees, saveDept } from "@/api/modules/department";
import { ResultEnum } from "@/enums/httpEnum";
import { DepartmentSchema } from "@/schema";
import { EmitProps } from "@/typings/interface";
import { isNotEmpty } from "@/utils/isHelper";
import { ElNotification, FormInstance } from "element-plus";
import { reactive, watch } from "vue";
import { nextTick } from "vue";
import { ref } from "vue";

const visible = ref(false);

const deptOpt = ref<Array<DepartmentSchema>>([]);
const formRef = ref<FormInstance>();

const modelValue = reactive<DepartmentSchema>({
  deptName: "",
  deptCode: "",
  parent: { id: "" } as DepartmentSchema
});

const rules = {
  deptName: [{ message: "请输入部门名称", trigger: "blur", required: true }],
  deptCode: [{ message: "请输入部门代码", trigger: "blur", required: true }]
};

function submit() {
  formRef.value?.validate().then(valid => {
    if (valid) {
      if (modelValue.id) {
        edit();
      } else {
        save();
      }
    }
  });
}

const emit = defineEmits<EmitProps>();

function edit() {
  editDept({ id: modelValue.id }, modelValue).then(r => {
    const { code, message } = r;
    if (code === ResultEnum.SUCCESS) {
      visible.value = false;
      emit("success");
      ElNotification.success("添加成功！");
    } else {
      ElNotification.error(message);
    }
  });
}

function save() {
  saveDept(modelValue).then(r => {
    const { code, message } = r;
    if (code === ResultEnum.SUCCESS) {
      visible.value = false;
      emit("success");
      ElNotification.success("编辑成功！");
    } else {
      ElNotification.error(message);
    }
  });
}

defineExpose({
  open(row) {
    visible.value = true;
    if (isNotEmpty(row)) {
      fetchDept(row);
    }
    fetchDeptTree();
  }
});

function fetchDeptTree() {
  fetchDeptTrees().then(r => {
    const { code, data, message } = r;
    if (code === ResultEnum.SUCCESS) {
      deptOpt.value = [];
      deptOpt.value = deptOpt.value.concat(data.list);
    } else {
      ElNotification.error(message);
    }
  });
}

function fetchDept(row) {
  fetchDeptInfo({ id: row.id }).then(r => {
    const { code, data, message } = r;
    if (code === ResultEnum.SUCCESS) {
      nextTick(() =>
        Object.assign(modelValue, {
          ...data,
          parent: data.parent ?? { id: "" }
        })
      );
    } else {
      ElNotification.error(message);
    }
  });
}

watch(
  () => visible.value,
  newVal => {
    if (!newVal) {
      nextTick(() => {
        formRef.value?.resetFields();
        modelValue.parent = { id: "" } as DepartmentSchema;
        modelValue.id = "";
      });
    }
  }
);
</script>
